﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogic.DTO.Test;
using BusinessLogic.Repository_Logic.Interfaces;
using TestSystem.Domain.Abstract;
using TestSystem.Domain.Entities.Questions;
using TestSystem.Domain.Repositories;

namespace BusinessLogic.Repository_Logic.Concentrate
{
    public class QuestionRepositoryLogic : IQuestionRepositoryLogic
    {
        private readonly IQuestionRepository _questionRepository = new QuestionRepository();
        private readonly ICategoryRepository _categoryRepository = new CategoriesRepository();

        public QuestionDTO GetQuestionByID(int questionID)
        {
            return new QuestionDTO
                       {
                           Question = _questionRepository.Questions.FirstOrDefault(q => q.QuestionID == questionID),
                           ChoiceQuestionAnwsers = _questionRepository.ChoiceQuestionAnwsers.Where(q => q.QuestionID == questionID).ToList(),
                           OpenQuestionAnwser = _questionRepository.OpenQuestionAnwsers.FirstOrDefault(q => q.QuestionID == questionID)
                       };
        }

        public QuestionDTO GetEmptyQuestionDTO()
        {
            var questionDto = new QuestionDTO()
                                  {
                                      Question = new Question(),
                                      ChoiceQuestionAnwsers = new List<ChoiceQuestionAnwser>(),
                                      OpenQuestionAnwser = new OpenQuestionAnwser(),
                                      Categories = _categoryRepository.Categories.ToList(),
                                      Difficulties = _questionRepository.Difficulties.ToList(),
                                      QuestionTypes = _questionRepository.QuestionTypes.ToList()
                                  };
            questionDto.AddChoiceQuestionAnwser();
            questionDto.AddChoiceQuestionAnwser();
            return questionDto;
        }

        public List<Question> GetAllQuestions()
        {
            return _questionRepository.Questions.ToList();
        }

        public void SaveQuestion(QuestionDTO questionDto)
        {
            _questionRepository.AddQuestion(questionDto.Question);
            if (questionDto.Question.QuestionTypeID.Equals(2))
                _questionRepository.AddChoiceQuestionAnwsers(questionDto.ChoiceQuestionAnwsers);
        }

        public void RemoveQuestion(int questionId)
        {
            var question = _questionRepository.Questions.Single(q => q.QuestionID.Equals(questionId));

            var choiceAnwsers =
                _questionRepository.ChoiceQuestionAnwsers.Where(choice => choice.QuestionID.Equals(questionId)).ToList();
            var openAnwsers = _questionRepository.OpenQuestionAnwsers.Where(open => open.QuestionID.Equals(questionId)).ToList();

            choiceAnwsers.ForEach(choiceAnwser => _questionRepository.RemoveChoiceQuestionAnwsers(choiceAnwser));
            openAnwsers.ForEach(openAnwser => _questionRepository.RemoveOpenQuestionAnwsers(openAnwser));

            _questionRepository.RemoveQuestion(question);
        }

        public int QuestionAmount()
        {
            return _questionRepository.Questions.Count();
        }
    }
}
